package com.eastjet.equipment.util;


import com.eastjet.equipment.service.DeviceRulesService;
import com.eastjet.maintenance.domain.DeviceRules;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

/**
 * 静态编码生成规则
 * @author: gunning
 */
@Component
public class CodeRules {


    /**
     * 返回生成的编码规则code
     */
    public String ReurnCode;   //生成的编码规则code
    private String tableName;   //表名
    public List<DeviceRules> CodeRules;  //规则列表
    LocalDate currentDate = LocalDate.now();
    public  String  NowTime= String.valueOf(currentDate.getYear())+String.valueOf(currentDate.getMonthValue())+String.valueOf(currentDate.getDayOfMonth());
    @Autowired
    private DeviceRulesService deviceRulesService;
    /**
     * 根据表名生成编码规则
     *
     * @param rules: 规则列表
     */
    public String codeRules(List<DeviceRules> rules) {
        System.out.println("rules:" + rules);
        CodeRules = rules;
        System.out.println(121313123);
        setTableName(rules.get(0).getRulesFromname());
        System.out.println(getTableName());
        getCurrentDatePart(rules.get(0).getStep());
        System.out.println("code********************:"+ReurnCode);
        return ReurnCode;
    }
    /**
     * 获取系统时间的指定部分
     *
     * @param part: 0-年 1-月 2-日 3-空
     * @return: String
     */
    public String getCurrentDatePart(int part) {
        System.out.println("part:" + part);

        String TimeType; //步长类型
        switch (part) {
            case 0:
                TimeType = String.valueOf(currentDate.getYear());
                break;
            case 1:
                TimeType = String.valueOf(currentDate.getMonthValue());

                break;
            case 2:
                TimeType = String.valueOf(currentDate.getDayOfMonth());
                break;
            default:
                TimeType = String.valueOf(currentDate.getMonthValue());
                System.out.println("TimeType:" + TimeType);
                System.out.println("tableName:" + getTableName());
                break;
        }
        return getCode(TimeType);
    }


    /**
     * code生成
     */
    public String getCode(String TimeType) {


        int NumLength = String.valueOf(CodeRules.get(0).getRulesNum()).length(); //获取规则后缀编码长度
        System.out.println("NumLength:" + NumLength);
        int LetterLength = CodeRules.get(0).getRulesLetter().length(); //获取规则前缀编码长度
        System.out.println("LetterLength:" + LetterLength);
        String TimeContent = getTimeType(CodeRules.get(0).getRulesTimeType()); //获取时间类型格式化字符串
        System.out.println("TimeContent:" + TimeContent);
        System.out.println("tableName:" + getTableName());
        String BigCode = deviceRulesService.getLastCode(getTableName()); //获取表名对应的最后一条编码
        System.out.println("BigCode:" + BigCode);
        String NumLast = getNum(CodeRules.get(0).getRulesNum()); //获取编码后缀
        System.out.println("NumLast:" + NumLast);
        String Letter = CodeRules.get(0).getRulesLetter(); //获取编码前缀
        System.out.println("Letter:" + Letter);


        // 如果BigCode为空，则生成新的编码
        if (BigCode == null || BigCode.isEmpty())
        {
            System.out.println("经过判断，BigCode为空");
            String time;
            System.out.println("CodeRules.get(0).getRulesTimeType():"+CodeRules.get(0).getRulesTimeType());
            System.out.println("getTimeType(CodeRules.get(0).getRulesTimeType()).length():"+getTimeType(CodeRules.get(0).getRulesTimeType()).length());
            switch (CodeRules.get(0).getRulesTimeType())
            {
                case 0:   time = String.valueOf(currentDate.getYear());
                    break;
                case 1:
                    if (currentDate.getMonthValue()<10)
                    {
                        time = String.valueOf(currentDate.getYear())+"0"+String.valueOf(currentDate.getMonthValue());
                    }else
                    {
                        time = String.valueOf(currentDate.getYear())+String.valueOf(currentDate.getMonthValue());
                    }
                    break;
                case 2:

                    if (currentDate.getMonthValue()<10){
                        if (currentDate.getDayOfMonth()<10)
                        {
                            time = String.valueOf(currentDate.getYear())+"0"+String.valueOf(currentDate.getMonthValue())+"0"+String.valueOf(currentDate.getDayOfMonth());
                        }else
                        {
                            time = String.valueOf(currentDate.getYear())+"0"+String.valueOf(currentDate.getMonthValue())+String.valueOf(currentDate.getDayOfMonth());
                        }

                    }else
                    {
                        if (currentDate.getDayOfMonth()<10){
                            time = String.valueOf(currentDate.getYear())+String.valueOf(currentDate.getMonthValue())+"0"+String.valueOf(currentDate.getDayOfMonth());
                        }else
                        {
                            time = String.valueOf(currentDate.getYear())+String.valueOf(currentDate.getMonthValue())+String.valueOf(currentDate.getDayOfMonth());
                        }
                    }

                    break;
                case 3:
                    time = time = String.valueOf(currentDate.getYear())+String.valueOf(currentDate.getMonthValue())+String.valueOf(currentDate.getDayOfMonth())+String.valueOf(currentDate.atStartOfDay().getHour());
                    break;
                case 4: time = time = String.valueOf(currentDate.getYear())+String.valueOf(currentDate.getMonthValue())+String.valueOf(currentDate.getDayOfMonth())+String.valueOf(currentDate.atStartOfDay().getHour())+String.valueOf(currentDate.atStartOfDay().getMinute());
                    break;
                default:time="";
                    break;
            }

            ReurnCode = Letter + time+ getNum(CodeRules.get(0).getRulesNum());
            return ReurnCode;
        }


        String Code;

        // 提取编码中间的部分
        // 假设编码格式为：前缀 + 中间部分 + 后缀
        // 中间部分开始的位置是前缀长度之后，结束的位置是整个编码长度减去后缀长度
        int middleStart = LetterLength;
        int middleEnd = BigCode.length() - NumLast.length();
        String middleCode = BigCode.substring(middleStart, middleEnd);


        // 从BigCode的末尾截取与NumLast长度相同的字符串
        String endSuffix = BigCode.substring(BigCode.length() - NumLast.length());
        System.out.println("endSuffix:" + endSuffix);  //后缀

        System.out.println("LetterLength:" + LetterLength); //前缀长度
        System.out.println("NumLength:" + NumLast.length());//后缀长度
        System.out.println("BigCode:" + BigCode);//编码
        System.out.println("middleCode:" + middleCode);//中间部分
        System.out.println("middleCodelength:" + middleCode.length());//中间部分长度




        if (middleCode.length() == 0) {
            String incrementedNum = getIncrementedNum(endSuffix); // 将后缀数字加1
            Code = CodeRules.get(0).getRulesLetter() + incrementedNum;
            System.out.println("Code:" + Code);  //生成的编码
            ReurnCode = Code;
            return ReurnCode;
        } else {
            System.out.println("TimeType123:" + TimeType);//时间类型(年月日)
            int timetype = 0;
            System.out.println("CodeRules.get(0).getStep():"+CodeRules.get(0).getStep());
            switch (CodeRules.get(0).getStep()) {
                case 0:
                    timetype = 0;
                    break;
                case 1:
                    timetype = 1;
                    break;
                case 2:
                    timetype = 2;
                    break;
                default:
                    timetype = 4;
                    break;
            }
          return   TimeCode(timetype, TimeType,middleCode,endSuffix);
        }
    }

    public String TimeCode(int timetype, String TimeType ,String middleCode,String endSuffix) {

        String returnCode = ""; //
        // 假设这是最终要返回的编码字符串
        String  Time="";
        System.out.println("timetype:"+timetype);
        switch (timetype) {
            case 0:
                // 保留middleCode的前4个字符
                if (middleCode.length() >= 4) {
                    returnCode = middleCode.substring(0, 4);
                    Time = String.valueOf(currentDate.getYear());
                }
                break;
            case 1:
                // 如果middleCode长度为8，去掉前4个字符，保留后面两个字符
                if (middleCode.length() >= 7 )   {
                    returnCode = middleCode.substring(0, 6);
                    if (currentDate.getMonthValue()<10)
                    {
                        Time = String.valueOf(currentDate.getYear())+"0"+String.valueOf(currentDate.getMonthValue());
                    }else
                    {
                        Time = String.valueOf(currentDate.getYear())+String.valueOf(currentDate.getMonthValue());
                    }
                }
                break;
            case 2:

                if (middleCode.length() >= 2) {
                    returnCode = middleCode.substring(0, middleCode.length());
                    if (currentDate.getMonthValue()<10){
                        if (currentDate.getDayOfMonth()<10)
                        {
                            Time = String.valueOf(currentDate.getYear())+"0"+String.valueOf(currentDate.getMonthValue())+"0"+String.valueOf(currentDate.getDayOfMonth());
                        }else
                        {
                            Time = String.valueOf(currentDate.getYear())+"0"+String.valueOf(currentDate.getMonthValue())+String.valueOf(currentDate.getDayOfMonth());
                        }

                    }else
                    {
                        if (currentDate.getDayOfMonth()<10){
                            Time = String.valueOf(currentDate.getYear())+String.valueOf(currentDate.getMonthValue())+"0"+String.valueOf(currentDate.getDayOfMonth());
                        }else
                        {
                            Time = String.valueOf(currentDate.getYear())+String.valueOf(currentDate.getMonthValue())+String.valueOf(currentDate.getDayOfMonth());
                        }
                    }
                }
                break;
        }
        System.out.println("time:"+Time);
        // 如果returnCode和TimeType都不为空，比对它们的大小
        if (!returnCode.isEmpty() && !Time.isEmpty()) {
            int returnCodeInt = Integer.parseInt(returnCode); // 将returnCode转换为整数
            int timeTypeInt = Integer.parseInt(Time); // 将TimeType转换为整数
            System.out.println("returnCodeInt:" + returnCodeInt);
            System.out.println("timeTypeInt:" + timeTypeInt);
            // 比较两个整数的大小
            if (returnCodeInt >= timeTypeInt) {
                // 如果returnCode小于等于TimeType，则执行某些操作
                String incrementedNum = getIncrementedNum(endSuffix); // 将后缀数字加1
                // 如果returnCode大于TimeType，则执行其他操作
                ReurnCode = CodeRules.get(0).getRulesLetter() + NowTime+incrementedNum;
                System.out.println("ReurnCode小于等于:" + ReurnCode);
            } else {
                ReurnCode=CodeRules.get(0).getRulesLetter() +NowTime + getNum(CodeRules.get(0).getRulesNum());
                System.out.println("ReurnCode大于:" + ReurnCode);
            }
        }
        return ReurnCode;
    }


    /**
     * 获取编码后缀
     */
    public String getIncrementedNum(String numStr) {
        System.out.println("numStr:" + numStr);
        // 将字符串转换为整数
        int num = Integer.parseInt(numStr);
        System.out.println("num:" + num);
        // 增加1
        num += 1;
        // 将整数转换回字符串，使用String.format确保前导零被保留
        return String.format("%0" + numStr.length() + "d", num);

    }


    /**
     * 获取编码后缀
     *
     * @param part: 0-001 1-0001 2-00001 3-空
     */
    public String getNum(int part) {
        switch (part) {
            case 0:
                return "001";
            case 1:
                return "0001";
            case 2:
                return "00001";
            default:
                return "";
        }
    }

    /**
     * 获取时间类型
     *
     * @param part: 0-年 1-月 2-日 3-时 4-分
     */
    public String getTimeType(int part) {
        switch (part) {
            case 0:
                return "yyyy";
            case 1:
                return "yyyymm";
            case 2:
                return "yyyymmdd";
            case 3:
                return "yyyymmddhh";
            case 4:
                return "yyyymmddhhmm";
            default:
                return "";
        }
    }


    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }
}
